package com.itheima.controller;

import com.itheima.config.aspect.Mylog;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import com.itheima.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/emps")

public class EmpController {
    @Autowired
    private EmpService empService;

    @Autowired
    private EmpMapper empMapper;
    /**
     * 请求分页数据
     * @param page
     * @param pageSize
     * @return
     */
//    @GetMapping
//    public Result selectByPage(@RequestParam(defaultValue = "0") Integer page,
//                                 @RequestParam(defaultValue = "3") Integer pageSize){
//        PageBean pageBean=empService.selectByPage(page,pageSize);
//        return Result.success(pageBean);
//    }

    /**
     * 使用分页插件 查询数据 能够 自动在代码最后加上limit数据
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result selectByPages(String name, String gender, LocalDate begin,
                                  LocalDate end,Integer page,Integer pageSize){
        PageBean pageBean=empService.selectByPages(name,gender,begin,end,page,pageSize);
        return Result.success(pageBean);
    }

    /**
     * 删除员工
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids){
        //根据多个id 删除
//        if (true){
//            throw new Error();
//        }
        empService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 插入员工
     * @param emp
     * @return
     */
    @PostMapping
    public Result insertByList(@RequestBody Emp emp){

        empService.insertByList(emp);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
            List<Emp> list=empMapper.selectByid(id);
            return Result.success(list);
    }




}
